package com.heima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.ApUserRealnameMapper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.UserRealname;
import com.heima.service.ApUserRealnameService;
import org.springframework.stereotype.Service;

/**
 * @author chenshan
 * @description 针对表【ap_user_realname(APP实名认证信息表)】的数据库操作Service实现
 * @createDate 2022-10-25 19:41:30
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, UserRealname>
        implements ApUserRealnameService {

    /**
     * 查询用户审核列表
     *
     * @param authDto 查询条件
     * @return ResponseResult
     */
    @Override
    public ResponseResult listUserRealname(AuthDto authDto) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(authDto.getPage())) {
            authDto.setPage(1);
        }

        if (ObjectUtil.isEmpty(authDto.getSize())) {
            authDto.setSize(10);
        }

        // 开启分页
        Page<UserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());

        page(page, Wrappers.lambdaQuery(UserRealname.class).eq(ObjectUtil.isNotEmpty(authDto.getStatus()), UserRealname::getStatus, authDto.getStatus()));

        PageResponseResult result = new PageResponseResult();
        result.setData(page.getRecords());
        result.setCurrentPage((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setTotal((int) page.getTotal());
        result.setHost("");

        return result;
    }

    /**
     * 用户审核通过
     *
     * @param id  用户id
     * @param msg 驳回原因
     * @return ResponseResult
     */
    @Override
    public ResponseResult authPass(Integer id, String msg) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 修改审核信息
        UserRealname realname = new UserRealname();
        realname.setId(id);
        realname.setReason(msg);
        realname.setStatus(9);
        updateById(realname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户审核驳回
     *
     * @param id  用户id
     * @param msg 驳回原因
     * @return ResponseResult
     */
    @Override
    public ResponseResult authFail(Integer id, String msg) {
        // 校验参数的合法性
        if (!ObjectUtil.isAllNotEmpty(id, msg)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 修改审核信息
        UserRealname realname = new UserRealname();
        realname.setId(id);
        realname.setReason(msg);
        realname.setStatus(2);
        updateById(realname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




